﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace PSE.Deployment.SetupBase.Configuration.Actions
{
    [Serializable]
    public class InstallWcfService : ActionBase
    {
        public List<WcfService> Services { get; set; }

        public InstallWcfService()
        {
            Services = new List<WcfService>();
        }

        public override bool HasFile(string filePath)
        {
            if (string.IsNullOrEmpty(filePath) || this.Services == null || this.Services.Count == 0)
                return false;
            else
            {
                //TODO:CODIGO MARRETADO - Quando for alterado para o filePath conter o id do sistema, remover a utilizacao do metodo RemoveRootLevel
                foreach (WcfService service in this.Services)
                {
                    if (service.Folder.Files != null && service.Folder.Files.Count > 0)
                    {
                        foreach (string file in service.Folder.Files)
                        {
                            if (PSE.Deployment.Util.File.RemoveRootLevel(file).Equals(filePath, StringComparison.CurrentCultureIgnoreCase))
                                return true;
                        }
                    }
                    else
                    {
                        if (filePath.StartsWith(PSE.Deployment.Util.File.RemoveRootLevel(service.Folder.Destination), StringComparison.CurrentCultureIgnoreCase))
                            return true;
                    }
                }

                return false;
            }
            //{
            //    return this.Services.Exists(s => ((s.Folder.Files != null && s.Folder.Files.Count > 0) && s.Folder.Files.Contains(filePath, StringComparer.CurrentCultureIgnoreCase)) ||
            //                                     ((s.Folder.Files == null || s.Folder.Files.Count == 0) && filePath.StartsWith(s.Folder.Destination.Remove(0, 1 + s.Folder.Destination.IndexOf(Path.DirectorySeparatorChar)), StringComparison.CurrentCultureIgnoreCase))
            //                                );
            //}
        }
    }
}